import { RedisOptions } from 'ioredis';

// 基础配置类型
export interface RedisConfig extends RedisOptions {
  host: string;
  port: number;
  password?: string;
  db?: number;
  keyPrefix?: string;
  autoSerialize?: boolean;
  retryAttempts?: number;
  retryDelay?: number;
  connectionTimeout?: number;
  logger?: Logger;
}

// 集群配置
export interface ClusterConfig extends RedisConfig {
  nodes: Array<{ host: string; port: number }>;
  enableReadyCheck?: boolean;
  maxRetriesPerRequest?: number;
}

// 日志接口
export interface Logger {
  debug(message: string, meta?: any): void;
  info(message: string, meta?: any): void;
  warn(message: string, meta?: any): void;
  error(message: string, meta?: any): void;
  createChild?(context: string): Logger;
}

// 重试策略
export interface RetryStrategy {
  maxAttempts: number;
  backoffType: 'exponential' | 'linear' | 'fixed';
  baseDelay: number;
  maxDelay: number;
  retryCondition?: (error: Error) => boolean;
}

// 操作结果
export interface OperationResult<T = any> {
  success: boolean;
  data?: T;
  error?: Error;
  duration?: number;
}

// 批量操作
export interface BatchOperation {
  command: string;
  args: any[];
}

// 中间件接口
export interface Middleware {
  before?(operation: Operation): Promise<void>;
  after?(operation: Operation, result: any): Promise<void>;
  error?(operation: Operation, error: Error): Promise<void>;
}

// 操作信息
export interface Operation {
  command: string;
  args: any[];
  key?: string;
  timestamp: number;
  duration?: number;
}

// 插件接口
export interface RedisPlugin {
  name: string;
  version: string;
  install(toolkit: any): void;
  uninstall(): void;
}

// 健康状态
export interface HealthStatus {
  status: 'healthy' | 'unhealthy' | 'degraded';
  uptime: number;
  connections: {
    active: number;
    total: number;
  };
  memory: {
    used: number;
    peak: number;
  };
  lastCheck: Date;
}

// 连接统计
export interface ConnectionStats {
  totalConnections: number;
  activeConnections: number;
  idleConnections: number;
  failedConnections: number;
  averageResponseTime: number;
}

// 性能指标
export interface Metric {
  name: string;
  value: number;
  timestamp: Date;
  tags?: Record<string, string>;
}

// 时间范围
export interface TimeRange {
  start: Date;
  end: Date;
}

// 慢查询
export interface SlowQuery {
  command: string;
  args: any[];
  duration: number;
  timestamp: Date;
}

// 导出数据
export interface ExportData {
  keys: Array<{
    key: string;
    type: string;
    value: any;
    ttl?: number;
  }>;
  metadata: {
    exportTime: Date;
    totalKeys: number;
    database: number;
  };
}

// 审计操作
export interface AuditOperation {
  operation: string;
  resource: string;
  user: string;
  timestamp: Date;
  success: boolean;
  details?: any;
}

// 内存报告
export interface MemoryReport {
  totalMemory: number;
  usedMemory: number;
  peakMemory: number;
  fragmentation: number;
  keyCount: number;
  averageKeySize: number;
}

// 性能分析结果
export interface ProfileResult<T> {
  result: T;
  duration: number;
  memoryUsage: {
    before: number;
    after: number;
    peak: number;
  };
  operations: Operation[];
}

// 键空间可视化
export interface KeyspaceVisualization {
  totalKeys: number;
  keysByType: Record<string, number>;
  keysByPattern: Array<{
    pattern: string;
    count: number;
    sampleKeys: string[];
  }>;
  memoryUsage: Record<string, number>;
}

// 命令信息
export interface Command {
  name: string;
  args: any[];
  timestamp: Date;
  duration?: number;
  result?: any;
  error?: Error;
}

// 备份信息
export interface BackupInfo {
  id: string;
  timestamp: Date;
  size: number;
  keyCount: number;
  database: number;
  checksum: string;
}

// 部署配置
export interface DeploymentConfig {
  redis: RedisConfig;
  monitoring: MonitoringConfig;
  logging: LoggingConfig;
  security: SecurityConfig;
  performance: PerformanceConfig;
}

export interface MonitoringConfig {
  enabled: boolean;
  metricsInterval: number;
  healthCheckInterval: number;
  slowQueryThreshold: number;
}

export interface LoggingConfig {
  level: 'debug' | 'info' | 'warn' | 'error';
  format: 'json' | 'text';
  destination: 'console' | 'file' | 'both';
  filePath?: string;
}

export interface SecurityConfig {
  encryption: {
    enabled: boolean;
    algorithm: string;
    key: string;
  };
  accessControl: {
    enabled: boolean;
    rules: AccessRule[];
  };
  audit: {
    enabled: boolean;
    logAllOperations: boolean;
  };
}

export interface PerformanceConfig {
  connectionPool: {
    min: number;
    max: number;
    acquireTimeout: number;
  };
  batchSize: number;
  pipelineSize: number;
  compressionThreshold: number;
}

export interface AccessRule {
  user: string;
  operations: string[];
  resources: string[];
  conditions?: Record<string, any>;
}
